package com.xw.Mail.Sender;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.ImageHtmlEmail;
import org.apache.commons.mail.resolver.DataSourceUrlResolver;

import com.xw.Beans.MailBox;
import com.xw.Beans.SendingMail;
import com.xw.Enums.MailSmtpTypeEnums;
import com.xw.Mail.Model.MailMessage;
import com.xw.Mail.listener.SendMailTransportListener;
import com.xw.Model.api.MailAttachment;

/**
 * 描述:封装common-mail的发送邮件类
 * @author 杨建全
 * @date 2017年5月26日 下午1:51:26
 */
public class CommonMailSender {
	
	private SendMailTransportListener sendMailListener;
	private MailBox mailbox;
	
	public CommonMailSender(MailBox mailbox,SendMailTransportListener sendMailListener) {
		super();
		this.sendMailListener = sendMailListener;
		this.mailbox = mailbox;
	}

	/**
	 * 描述：异步带监听发送邮件
	 * @author 杨建全
	 * @date 2017年6月7日
	 * @param sendingMail
	 * @throws EmailException
	 * @throws MessagingException
	 * @throws MalformedURLException
	 */
	public void sendMailAsync(SendingMail sendingMail) throws EmailException,MessagingException, MalformedURLException{
		ImageHtmlEmail mail = buildHtmlEmail(sendingMail);
		//构建邮件对象
		mail.buildMimeMessage();
		MimeMessage mimeMessage = mail.getMimeMessage();
		MailMessage mailMsg = new MailMessage(mimeMessage) ;
		mailMsg.setId(sendingMail.getId());
		//获取发送对象
		Transport transport = mail.getMailSession().getTransport("smtp");
		transport.addTransportListener(sendMailListener);
		//打开链接
		if (!transport.isConnected()) {
			transport.connect();
		}
		//发送
		transport.sendMessage(mailMsg, mimeMessage.getAllRecipients());
	}
	
	/**
	 * 描述：同步方式发送邮件
	 * @author 杨建全
	 * @date 2017年6月7日
	 * @param sendingMail
	 * @throws MalformedURLException
	 * @throws EmailException
	 */
	public void sendMail(SendingMail sendingMail) throws MalformedURLException, EmailException{
		ImageHtmlEmail mail = buildHtmlEmail(sendingMail);
		mail.send();
	}

	/**
	 * 描述：将sendingMail构建成ImageHtmlEmail
	 * @author 杨建全
	 * @date 2017年6月7日
	 * @param sendingMail
	 * @return
	 * @throws EmailException
	 * @throws MalformedURLException
	 */
	private ImageHtmlEmail buildHtmlEmail(SendingMail sendingMail) throws EmailException, MalformedURLException {
		ImageHtmlEmail mail = new ImageHtmlEmail();
		
		mail.setSmtpPort(Integer.parseInt(mailbox.getSmtpPort()));
		this.setSmtpType(mail);
		mail.setCharset("UTF-8");
		mail.setSubject(sendingMail.getSubject());
		mail.setAuthentication(mailbox.getMailBoxName(),mailbox.getPassword());
		mail.setHtmlMsg(sendingMail.getHtml());
		mail.setHostName(mailbox.getSmtpUrl());
		mail.setFrom(sendingMail.getFromUser());
		mail.setHeaders(sendingMail.getMailHeader());
		
		URL url = new URL("http://");
		mail.setDataSourceResolver(new DataSourceUrlResolver(url));
		
		List<String> bccUsers = sendingMail.getBccUsers();
		if (bccUsers!=null && !bccUsers.isEmpty()) {
			mail.addBcc(bccUsers.toArray(new String[bccUsers.size()]));
		}
		
		List<String> ccUsers = sendingMail.getCcUsers();
		if (ccUsers!=null && !ccUsers.isEmpty()) {
			mail.addCc(ccUsers.toArray(new String[ccUsers.size()]));
		}
		
		List<String> toUsers = sendingMail.getToUsers();
		if (toUsers!=null && !toUsers.isEmpty()) {
			mail.addTo(toUsers.toArray(new String[toUsers.size()]));
		}
		//如果有附件
		if (null!=sendingMail.getAttachmentList()) {
			for (MailAttachment mainAttachment : sendingMail.getAttachmentList()) {
				EmailAttachment attachment = new EmailAttachment();
				attachment.setURL(new URL(mainAttachment.getUrl()));
				attachment.setDisposition(EmailAttachment.ATTACHMENT);
				attachment.setDescription(mainAttachment.getDescription());
				attachment.setName( mainAttachment.getName());
				mail.attach(attachment);
			}
		}
		return mail;
	}

	/**
	 * 描述：设置smtp发送链接类型
	 * @author 杨建全
	 * @date 2017年11月17日
	 * @param mail
	 */
	private void setSmtpType(ImageHtmlEmail mail) {
		Integer smtpType = mailbox.getSmtpType();
		switch (smtpType) {
			case MailSmtpTypeEnums.NOSSL:
				break;
			case MailSmtpTypeEnums.SSL:
				mail.setSSLOnConnect(true);
				break;
			case MailSmtpTypeEnums.STARTTLS:
				mail.setStartTLSEnabled(true);
				break;
			default:
				break;
		}
	}
}
